package edu.xlh.travel.service.impl;

import edu.xlh.travel.common.Result;
import edu.xlh.travel.dao.CommentDao;
import edu.xlh.travel.pojo.Comment;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.service.CommentService;
import edu.xlh.travel.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private IdWorker idWorker;
    @Override
    public Result comment(Comment comment , HttpSession session) {
        User user = (User) session.getAttribute("user");
        String name = user.getName();
        comment.setId(idWorker.nextId()+"");
        comment.setUserName(name);
        try {
            commentDao.save(comment);
        } catch(Exception e) {
            return new Result(201 , "评论失败");
        }
       return new Result(200 , "评论成功");
    }
}
